import urllib.request
import os


user = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
list1 = ['A','E','I','M','Q','U','Y','c','g','k']
def url_open(url):
        req = urllib.request.Request(url)
        req.add_header('User-Agent',user)
        response = urllib.request.urlopen(req)
        html = response.read()
        return html

def img_find(url):
    html=url_open(url).decode('utf-8')
    img_addr = []
    a=html.find('img src=')
    while a !=-1:
        b=html.find('.jpg',a,a+255)
        if b != -1:
            img_addr.append('http:'+html[a+9:b+4])
        else :
            b=a+9

        a=html.find('img src=',b)
    print(img_addr)
    return img_addr    
    

def save(folder,img_addr):
    for each in img_addr:
        name = each.split('/')[-1]
        with open(name,'wb') as f:
            img = url_open(each)
            f.write(img)
            
    

def download(folder='ooxx',page=2):
    os.mkdir(folder)
    os.chdir(folder)
    
    url = 'http://www.jandan.net/ooxx/MjAyMDAxMDctOD'

    for i in range(page):
        url =url + list1[i] + '=#comments'
        img_addr = img_find(url)
        save(folder,img_addr)
        url = 'http://www.jandan.net/ooxx/MjAyMDAxMDctOD'
        
    
if __name__ == '__main__':
    download()
